package cm309.BookingSystem.tm.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.security.ssl.Debug;

import cm309.BookingSystem.common.Common;
import cm309.BookingSystem.common.MyConnection;
import cm309.BookingSystem.tm.model.TM_FreeTimeTable;
import cm309.BookingSystem.web.Action;

public class ReadTTAction implements Action {
	List<TM_FreeTimeTable> ttbs=new ArrayList<TM_FreeTimeTable>();
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		MyConnection con=new MyConnection();
		try {
			String qTime=request.getParameter("date");	
			ResultSet rs=con.getPS("select RANGEID ,TIMEFROM ,TIMETO ,REMARK,TIMEFROM||' ~ '||TIMETO as leaseTime " +
					"from BK_TM_FREETIMETABLE where TimeFrom like '"+qTime+"%' order by TIMEFROM ")
			               .executeQuery();
			Common.logger.info("select RANGEID ,TIMEFROM ,TIMETO ,REMARK,TIMEFROM||' ~ '||TIMETO as leaseTime " +
					"from BK_TM_FREETIMETABLE where TimeFrom like '"+qTime+"%' order by TIMEFROM ");
			TM_FreeTimeTable ttb=new TM_FreeTimeTable();
			ttbs.clear();
			while (rs.next()) {
			ttb=new TM_FreeTimeTable();
			ttb.setRangeID(rs.getInt("RANGEID"));
			ttb.setTimeFrom(rs.getTimestamp("TIMEFROM"));
			ttb.setTimeTo(rs.getTimestamp("TIMETO"));
	        ttb.setRemark(rs.getString("REMARK"));
	        ttb.setLeaseTime(rs.getString("leaseTime"));
			ttbs.add(ttb);
			
			}
			con.commit();
            con.PSclose();
			PrintWriter w = response.getWriter();
			
			w.println(Common.beanToJson(ttbs));
			
			
		} catch (IOException e) {
			e.printStackTrace();
		
	    } catch (SQLException e) {
	  	
	    	e.printStackTrace();
	    }
		finally {
		  con.close();
		}

	}

}
